package com.hxzy.shop.order.product;

import com.hxzy.shop.entity.OrderTable;
import com.hxzy.shop.message.*;
import com.hxzy.shop.order.dto.OmsOrderDto;
import com.hxzy.shop.order.dto.OmsOrderReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @Author ht
 * @Date 2021/9/1 11:31
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderProduct {
    @Resource
    private OrderSource orderSource;
    private static final String ORDER_CHANGE = "orderSuccessChange";

    private static final String ORDER_DONE = "orderDone";

    private static final String CARE_PUSH = "carePush";

    private static final String ORDER_DRUG_OFFER_DETAILS = "orderDrugOfferDetails";


    private static final String DATAMANAGEMENT_AFTER_SALES_CHANGE = "datamanageAfterSales";

    private static final String MEMBER_LABEL_INNER = "memberLabelInner";

    private static final String OMS_MESSAGE_SEND = "omsMessageSend";

    private static final String OMS_REFUND_MESSAGE_SEND = "omsRefundMessageSend";

    private static final String OMS_EXPRESS_MESSAGE_SEND = "omsExpressMessageSend";

    private static final String ERP_ORDER_PUSH = "erpOrderPush";


    /**
     * 发送支付确认修改事件
     */
    public Boolean sendOrderChange(OrderChangeMessage message) {
        log.error("订单支付："+message.getOrderId());
        Message<OrderChangeMessage> msg = MessageBuilder.withPayload(message)
                .setHeader(RocketMQHeaders.KEYS, message.getOrderId() + "-" + message.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, ORDER_CHANGE).build();
        return orderSource.orderChangeOutput().send(msg);
    }

    /**
     * 发送订单完成事件
     */
    public Boolean sendOrderDone(OrderDoneMessage message) {
        log.error("订单支付："+message.getOrderId());
        Message<OrderDoneMessage> msg = MessageBuilder.withPayload(message)
                .setHeader(RocketMQHeaders.KEYS, message.getOrderId() + "-" + message.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, ORDER_DONE).build();
        return orderSource.orderChangeOutput().send(msg);
    }

    /**
     * 专业关怀推送
     */
    public Boolean sendCarePush(CarePushMessage message) {
        log.error("专业关怀推送："+message.getOrderId());
        Message<CarePushMessage> msg = MessageBuilder.withPayload(message)
                .setHeader(RocketMQHeaders.KEYS, message.getOrderId() + "-" + message.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, CARE_PUSH).build();
        return orderSource.carePushOutput().send(msg);
    }

    /**
     * 发送订单生成商品优惠明细
     */
    public Boolean sendOrderDrugOfferDetails(OrderDrugOfferDetailsMessage message) {
        log.error("订单商品优惠明细："+message.getOrderId());
        Message<OrderDrugOfferDetailsMessage> msg = MessageBuilder.withPayload(message)
                .setHeader(RocketMQHeaders.KEYS, message.getOrderId() + "-" + message.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, ORDER_DRUG_OFFER_DETAILS).build();
        return orderSource.orderChangeOutput().send(msg);
    }

    /**
     * 退单后修改当日的商户记录
     */

    public Boolean sendDatamanageAfterSales(OrderTable order){

        DataManageAfterSaleMessage message = new DataManageAfterSaleMessage();
        message.setMerchantId(order.getMerchantId());
        message.setOrderNo(order.getOrderNo());
        log.error("消息发送中：");
        Message<DataManageAfterSaleMessage> msg = MessageBuilder.withPayload(message)
                .setHeader(RocketMQHeaders.KEYS, message.getOrderNo() + "-" + message.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, DATAMANAGEMENT_AFTER_SALES_CHANGE).build();
        return orderSource.datamanageAfterSalesOutput().send(msg);
    }

    /**
     * 用户订单结算 打标签
     */
    public Boolean sendMemberLabel(OrderTable order){

        log.info("消息发送中：");
        Message<OrderTable> msg = MessageBuilder.withPayload(order)
                .setHeader(RocketMQHeaders.KEYS, order.getOrderNo() + "-" + order.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, MEMBER_LABEL_INNER).build();
        return orderSource.memberLabelInnerOutput().send(msg);
    }

    /**
     * OMS订单消息推送
     */
    public Boolean orderMessageSend(OmsOrderReq req){
        log.info("消息发送中：");
        Message<OmsOrderReq> msg = MessageBuilder.withPayload(req)
                .setHeader(RocketMQHeaders.KEYS, req.getOrderid() + "-" + req.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, OMS_MESSAGE_SEND).build();
        return orderSource.orderMessageSend().send(msg);
    }

    /**
     * OMS售后订单推送
     */
    public Boolean refundOrderSend(OmsOrderReq req){
        log.info("消息发送中：");
        Message<OmsOrderReq> msg = MessageBuilder.withPayload(req)
                .setHeader(RocketMQHeaders.KEYS, req.getOrderid() + "-" + req.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, OMS_REFUND_MESSAGE_SEND).build();
        return orderSource.refundOrderSend().send(msg);
    }

    /**
     * OMS快递信息推送
     */
    public Boolean expressMessageSend(OmsOrderReq req){
        log.info("消息发送中：");
        Message<OmsOrderReq> msg = MessageBuilder.withPayload(req)
                .setHeader(RocketMQHeaders.KEYS, req.getOrderid() + "-" + req.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, OMS_EXPRESS_MESSAGE_SEND).build();
        return orderSource.expressMessageSend().send(msg);
    }

    /**
     * ERP订单消息推送
     */
    public Boolean orderPush(OmsOrderDto req){
        log.info("消息发送中：");
        Message<OmsOrderDto> msg = MessageBuilder.withPayload(req)
                .setHeader(RocketMQHeaders.KEYS, req.getOrderId() + "-" + req.getMerchantId())
                .setHeader(MessageConst.PROPERTY_TAGS, ERP_ORDER_PUSH).build();
        return orderSource.orderPush().send(msg);
    }


}
